package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.LiaisonWay;
import com.prs.crm.service.crm.LiaisonWayService;
import com.prs.crm.service.sys.LogService;

public class LiaisonWayAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private LiaisonWay liaisonWay;
	private LiaisonWayService liaisonWayService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	
	@Action("listLiaisonWay")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(LiaisonWay.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLiaisonWayService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editLiaisonWay")
	public String edit() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveLiaisonWay",results={@Result(name="success",type="redirect",location="listLiaisonWay"),
			@Result(name="error",type="chain",location="editLiaisonWay")})
	public String save() {
		if(liaisonWay.getName()==null||"".equals(liaisonWay.getName().trim())){
			this.addActionError("方式不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(LiaisonWay.class, liaisonWay
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.setPrompt("联络方式提交成功!");
		this.getLiaisonWayService().save(liaisonWay);
		return SUCCESS;
	}
	
	@Action(value="removeLiaisonWay",results={@Result(name="success",type="redirect",location="listLiaisonWay"),
			@Result(name="error",type="chain",location="listLiaisonWay")})
	public String remove() {
		if(ids == null) {
			this.addActionError("请选择要删除的联络方式");
			return ERROR;
		}
		for(Integer id : ids) {
			LiaisonWay way = this.getLiaisonWayService().get(id);
			if(way.getLiaisons().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getLiaisonWayService().batchRemove(ids);
		return SUCCESS;
	}
	
	public LiaisonWay getLiaisonWay() {
		return liaisonWay;
	}
	public void setLiaisonWay(LiaisonWay liaisonWay) {
		this.liaisonWay = liaisonWay;
	}
	public LiaisonWayService getLiaisonWayService() {
		return liaisonWayService;
	}
	public void setLiaisonWayService(LiaisonWayService liaisonWayService) {
		this.liaisonWayService = liaisonWayService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
